home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / UTIL / PROGRAMMING / RINK010 / Demo / c / rinkdemo next >
Text File  |  1995-06-25  |  5KB  |  210 lines

  1. // rinkdemo - main program
  2.  
  3. // (c) Ben Summers 1995
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8.  
  9. #include "kernel.h"
  10. #include "rink.h"
  11.  
  12. // this demonstration of rink shows the recommended way of loading
  13. // and using rink segments. However, you don't have to do it like
  14. // this - the run time system just provides some basic routine
  15. // to handle segments, and doesn't force any method of use on you.
  16.  
  17. typedef int BOOL;
  18.  
  19. #define TRUE    1
  20. #define FALSE   0
  21.  
  22. // the function prototypes of the functions in segments.
  23.  
  24. typedef int (*SegmentStart)(char *, int);
  25. typedef void (*SegmentDo)(void);
  26. typedef int (*SegmentFinish)(int);
  27.  
  28. typedef void (*SegmentNamed)(void);
  29.  
  30. // and a structure to hold info about the segments we've loaded
  31.  
  32. typedef struct {
  33.     rink_seghandle Handle;
  34.     struct {
  35.         SegmentStart Start;       // there should be the same number
  36.         SegmentDo Do;             // of fns here that there are in the header
  37.         SegmentFinish Finish;     // and they must be in the same order
  38.     } fns;
  39. } SegmentInfo;
  40.  
  41. // a rink_check structure for loading segments
  42.  
  43. rink_check CheckBlock;
  44.  
  45.  
  46.  
  47.  
  48.  
  49. // an error wrapping function
  50.  
  51. BOOL ErrWrap(void *err, char *Doing)
  52. {
  53.     char *String = ((char *)err) + 4;
  54.  
  55.     if(err == 0)
  56.         return TRUE;
  57.  
  58.     printf("Error in %s: %s\n", Doing, String);
  59.  
  60.     return FALSE;
  61. }
  62.  
  63.  
  64.  
  65.  
  66. // some data for segments to access
  67.  
  68. int NumberInParent = 56;
  69.  
  70.  
  71.  
  72. // a function for the segments to call
  73.  
  74. int FunctionInParentProgram(int Number)
  75. {
  76.     printf("FunctionInParentProgram called, Number = %d\n\n", Number);
  77.  
  78.     return Number / 2;
  79. }
  80.  
  81.  
  82.  
  83.  
  84.  
  85. // a routine to load a segment. Takes a pointer to the name
  86. // of the directory the code and links files are stored and
  87. // a Segment info to put the interesting stuff in.
  88.  
  89. BOOL LoadSegment(SegmentInfo *pSegment, char *Name)
  90. {
  91.     char CodeName[256];
  92.     char LinksName[256];
  93.     _rink_fn *PtrArray;
  94.     rink_version *Version;
  95.     int Fn;
  96.  
  97.     // make filename of the code and links
  98.     sprintf(CodeName, "%s.Code", Name);
  99.     sprintf(LinksName, "%s.Links", Name);
  100.  
  101.     // load the segment...
  102.     if(!ErrWrap(rink_load(CodeName, LinksName, &pSegment->Handle, &CheckBlock), "segment load"))
  103.         return FALSE;
  104.  
  105.     // OK, let's have a look at the version of the segment we've just loaded.
  106.     // It might be nice to check them to see that it's acceptable.
  107.     // It is a bad plan to alter the returned structure.
  108.     Version = rink_readversion(pSegment->Handle);
  109.  
  110.     printf("Segment '%s': Main version = %d, Code version = %d\n\n", Name, Version->main, Version->code);
  111.  
  112.     // right, now get the function pointers...
  113.     PtrArray = (_rink_fn *)&pSegment->fns;
  114.     for(Fn = 0; Fn < (sizeof(pSegment->fns) / sizeof(_rink_fn)); Fn++)
  115.     {
  116.         // for each function, get a pointer from the run time system
  117.         PtrArray[Fn] = rink_fn(pSegment->Handle, Fn);
  118.  
  119.         // check that it's not zero - ie no function
  120.         if(PtrArray[Fn] == 0)
  121.         {
  122.             printf("Function not present in segment %s\n\n", Name);
  123.             return FALSE;
  124.         }
  125.     }
  126.  
  127.     // done
  128.  
  129.     return TRUE;
  130. }
  131.  
  132.  
  133.  
  134.  
  135.  
  136. // routine to unload segments
  137.  
  138. BOOL UnloadSegment(SegmentInfo *pSegment)
  139. {
  140.     // could call a finalise function here...
  141.  
  142.     // unload the segment from memory
  143.     rink_unload(pSegment->Handle);
  144.  
  145.     return TRUE;
  146. }
  147.  
  148.  
  149.  
  150.  
  151.  
  152. // the main function
  153.  
  154. int main(void)
  155. {
  156.     SegmentInfo Segment1;
  157.     SegmentInfo Segment2;
  158.     int l;
  159.     SegmentNamed Fn;
  160.     char *Name;
  161.  
  162.     printf("rink demonstration program\n(c) Ben Summers 1995\n\n");
  163.  
  164.     // set up the check block
  165.     strcpy(CheckBlock.id, "rinkdemo");
  166.     CheckBlock.main_version = 100;
  167.     CheckBlock.code_version = 0;
  168.  
  169.     // load the two segments
  170.     printf("Loading segments...\n\n");
  171.     if(!LoadSegment(&Segment1, "Segment1"))
  172.         return 1;
  173.  
  174.     if(!LoadSegment(&Segment2, "Segment2"))
  175.         return 1;
  176.  
  177.     printf("Segments loaded\n\n");
  178.  
  179.     // call a few functions... start with the start functions.
  180.     printf("Segment1 Start returned %d\n\n", Segment1.fns.Start("A nice little string", 42));
  181.     printf("Segment2 Start returned %d\n\n", Segment2.fns.Start("A pretty string", 19));
  182.  
  183.     // and the do functions
  184.     Segment1.fns.Do();
  185.     Segment2.fns.Do();
  186.  
  187.     // and finish off with a final
  188.     printf("Segment1 Finish returned %d\n\n", Segment1.fns.Finish(12));
  189.     printf("Segment2 Finish returned %d\n\n", Segment2.fns.Finish(90));
  190.  
  191.     // call some named functions in Segment1. Now, I'm not really sure what you'd
  192.     // do with them, but they're there just in case.
  193.  
  194.     printf("Calling named functions from Segment1...\n\n");
  195.     l = 0;
  196.     do {
  197.         Fn = (SegmentNamed)rink_enum_named(Segment1.Handle, &l, &Name);
  198.         printf("*** calling %s\n", Name);
  199.         Fn();
  200.     } while(l >= 0);
  201.  
  202.     // unload them now we've finished
  203.     UnloadSegment(&Segment1);
  204.     UnloadSegment(&Segment2);
  205.  
  206.     // all done.
  207.     return 0;
  208. }
  209.  
  210.